package com.ttxs.uaa.api.common;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttxs.common.core.utils.poi.ExcelUtil;
import com.ttxs.common.core.vo.R;
import com.ttxs.uaa.api.BaseApi;
import com.ttxs.common.core.constant.Api;
import com.ttxs.uaa.entity.SysUser;
import com.ttxs.uaa.service.ISysUserService;
import com.ttxs.uaa.vo.ImportUserVO;
import com.ttxs.uaa.vo.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 天天向上
 * @since 2021-07-26
 */
@RestController
@RequestMapping(Api.Common.User.PREFIX)
@RequiredArgsConstructor
public class UserApi extends BaseApi {
    private final ISysUserService sysUserService;

    @GetMapping
    public R list(@RequestParam("page") int pageNo,
                  @RequestParam("limit") int pageSize,
                  @RequestParam(value = "deptId", required = false) Long[] deptIds,
                  @RequestParam(value = "userName", required = false) String userName,
                  @RequestParam(value = "nickName", required = false) String nickName,
                  @RequestParam(value = "sex", required = false) String sex,
                  @RequestParam(value = "phonenumber", required = false) String phonenumber,
                  @RequestParam(value = "status", required = false) String status) {
        UserVO condition = new UserVO();
        condition.setPlatformId(getPlatformId());
        condition.setSex(sex);
        condition.setDeptIds(deptIds);
        condition.setUserName(userName);
        condition.setNickName(nickName);
        condition.setPhonenumber(phonenumber);
        condition.setStatus(status);
        Page<UserVO> page = sysUserService.pageVo(Page.of(pageNo, pageSize), condition);
        return R.ok(page);
    }

    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        SysUser sysUser = this.sysUserService.getWithRoles(id);
        return R.ok(sysUser);
    }

    @PostMapping
    public R add(@RequestBody SysUser sysUser) {
        sysUser.setPlatformId(getPlatformId());
        this.sysUserService.register(sysUser);
        return R.ok();
    }

    @PostMapping("/importData")
    public R importUsers(String platformName, MultipartFile file) throws Exception{
        ExcelUtil<ImportUserVO> excelUtil = new ExcelUtil<>(ImportUserVO.class);
        List<ImportUserVO> list = excelUtil.importExcel(file.getInputStream());
        sysUserService.importUsers(platformName, list);
        return R.ok();
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<ImportUserVO> util = new ExcelUtil<>(ImportUserVO.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @DeleteMapping("/{id}")
    public R del(@PathVariable("id") Long[] id) {
        this.sysUserService.removeByIds(id);
        return R.ok();
    }

    @PutMapping("/{id}")
    public R update(@PathVariable Long id, @RequestBody SysUser sysUser) {
        sysUser.setUserId(id);
        sysUser.setPlatformId(getPlatformId());
        this.sysUserService.updateByAim(sysUser);
        return R.ok();
    }

    @PutMapping(Api.Common.User.RESET_PASSWORD)
    public R resetPassword(@PathVariable Long id, String userName) {
        this.sysUserService.resetPassword(id, userName, getPlatformId());
        return R.ok();
    }



}
